package cmd

import (
	"fmt"
	"md5util/util"
	"os"
	"strings"
	"time"

	"github.com/olekukonko/tablewriter"
	"github.com/spf13/cobra"
)

// md5textCmd represents the md5text command
var md5textCmd = &cobra.Command{
	Use:   "text",
	Short: "文本MD5",
	Long:  `根据文本字符串生成MD5`,
	Run: func(cmd *cobra.Command, args []string) {
		upper, _ := cmd.Flags().GetBool("upper")
		table := tablewriter.NewWriter(os.Stdout)
		table.SetHeader([]string{"MD5", "SPENT(ms)", "SIZE"})
		defer table.Render()

		start := time.Now()
		md5str, bsize, err := util.MD5Text(strings.Join(args, " "), upper)
		spent := fmt.Sprintf("%d", time.Since(start).Milliseconds())
		if err != nil {
			fmt.Println(err)
		} else {
			table.Append([]string{md5str, spent, bsize})
		}
	},
}

func init() {
	rootCmd.AddCommand(md5textCmd)
}
